﻿Imports System.Windows.Forms
Imports System.Reflection
Public Class ProgessForm

#Region "构造函数"
    Public frm As CrystlViewer = Nothing
    Public Sub New(ByVal frm As CrystlViewer)
        ' 此调用是 Windows 窗体设计器所必需的。
        InitializeComponent()
        Me.frm = frm
        frm._进度窗体 = Me
    End Sub

#End Region

#Region "设置进度条"
    Public _Percent As Integer = 0
    Public Property Percent() As Integer
        Get
            Return _Percent
        End Get
        Set(ByVal value As Integer)
            _Percent = value
            SetProgressValue()
        End Set
    End Property
    Private Sub SetProgressValue()
        If Not Me.InvokeRequired() Then
            Me.Invoke(New EventHandler(AddressOf SetProgress))
            System.Windows.Forms.Application.DoEvents()
        End If
    End Sub

    Private Sub SetProgress(ByVal sender As System.Object, ByVal e As System.EventArgs)
        If ProgressBar1 IsNot Nothing Then
            Me.ProgressBar1.Value = Percent
        End If
    End Sub

#End Region

#Region "设置显示信息"
    Public _StrText As String = ""
    Public Property StrText() As String
        Get
            Return _StrText
        End Get
        Set(ByVal value As String)
            _StrText = value
            SetLableText()
        End Set
    End Property
    Private Sub SetLableText()
        If Not Me.InvokeRequired() Then
            Me.Invoke(New EventHandler(AddressOf SetLable))
            System.Windows.Forms.Application.DoEvents()
        End If
    End Sub

    Private Sub SetLable(ByVal sender As System.Object, ByVal e As System.EventArgs)
        If lstStatus IsNot Nothing Then
            Me.lstStatus.Text = StrText
        End If
    End Sub
#End Region

    Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
        '添加一个加载时的事件
        AddHandler frm.Load, AddressOf PROGRESS1_Load
        '添加一个窗体为活动时
        AddHandler frm.Activated, AddressOf PROGRESS1_Activated
        '添加一个显示时处理的事件
        AddHandler frm.Shown, AddressOf PROGRESS1_Shown
        '窗体关闭时处理的事件
        AddHandler frm.FormClosed, AddressOf PROGRESS1_FormClosed
        '显示窗体
        Dim di As System.Delegate = New EventHandler(AddressOf Show1)
        Me.Invoke(di)
    End Sub

#Region "窗体显示时 开始运行"
    Private Sub PROGRESS1_Shown_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Shown
        '窗体显示时 开始运行
        BackgroundWorker1.RunWorkerAsync()
        Percent = 10
        StrText = "开始处理......"
    End Sub
#End Region

    '弹出反射的窗体
    Public Sub Show1(ByVal sender As System.Object, ByVal e As System.EventArgs)
        '初始时不让显示
        frm.Opacity = 0
        System.Windows.Forms.Application.DoEvents()
        frm.Owner = Me.Owner
        Percent = 30
        StrText = "准备初始化窗体......"
        '设置父窗体
        If UTIL.gloabs.ParentForm IsNot Nothing Then
            frm.ShowDialog(UTIL.gloabs.ParentForm)
        Else
            frm.ShowDialog()
        End If
    End Sub

#Region "反射的窗体显示时"
    Private Sub PROGRESS1_Shown(ByVal sender As Object, ByVal e As System.EventArgs)
        Percent = 100
        SetProgressValue()
        '隐藏启动窗体
        Me.Visible = False
        System.Windows.Forms.Application.DoEvents()
        '显示窗体
        frm.Opacity = 1
        System.Windows.Forms.Application.DoEvents()

    End Sub
#End Region

#Region "反射的窗体关闭时"
    '反射的窗体关闭时 关闭等待窗体
    Private Sub PROGRESS1_FormClosed(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosedEventArgs)
        Try
            Me.BackgroundWorker1.Dispose()

            frm.Dispose()
            frm = Nothing
        Catch ex As Exception
            UTIL.CException.SaveExcepiton(UTIL.ErrorType.Error, ex, MethodBase.GetCurrentMethod().GetType())
        End Try
        Me.Close()
    End Sub
#End Region

#Region "反射的窗体关闭时"
    Private Sub PROGRESS1_Activated(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Percent = 75
        StrText = "窗体准备显示"
    End Sub
#End Region

#Region "反射的窗体加载LOAD 事件时"
    Private Sub PROGRESS1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Percent = 60
        StrText = "窗体初始化成功"
    End Sub
#End Region

#Region ""

#End Region

End Class
